package com.zznode.redsun.commons.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;

/**
 * 全局常量定义
 * 
 * @author taoping
 * 
 */
public final class GlobalUtils {
	private static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	private static final DateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	/**
	 * Bean属性Copy时应该排除的惯性
	 */
	public static final String[] DOMAINBUSINESS_IGNOREPROPERTIES = { "createDate", "isDeleted" };

	private GlobalUtils() {

	}

	/**
	 * pattern : yyyy-MM-dd
	 * 
	 * @param date
	 * @return
	 */
	public static String formartDate(Date date) {
		if (null == date) {
			return null;
		}
		return dateFormat.format(date);
	}

	/**
	 * pattern : yyyy-MM-dd HH:mm:ss
	 * 
	 * @param date
	 * @return
	 */
	public static String formartDateTime(Date date) {
		if (null == date) {
			return null;
		}
		return dateTimeFormat.format(date);
	}

	public synchronized static String generateCode(String prefix) {
		Calendar calendar = Calendar.getInstance();
		StringBuilder codeBuilder = new StringBuilder(null == prefix ? StringUtils.EMPTY : prefix.trim());
		codeBuilder.append(String.valueOf(calendar.get(Calendar.YEAR)).substring(2));
		codeBuilder.append(StringUtils.leftPad(String.valueOf(calendar.get(Calendar.MONTH)), 2, "0"));
		codeBuilder.append(StringUtils.leftPad(String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)), 2, "0"));
		codeBuilder.append(String.valueOf(calendar.getTimeInMillis()).substring(8));

		return codeBuilder.toString();
	}

}
